
//
//  LastStageViewController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/30.
//  Copyright © 2016年 czd. All rights reserved.
//
#define DEVICEWIDTH [UIScreen mainScreen].bounds.size.width
#define DEVICEHEIGHT [UIScreen mainScreen].bounds.size.height

#import "LastStageViewController.h"
#import "LastStageModel.h"
#import "LastStageViewCell.h"
#import "LastStageTabModel.h"
#import "LastStageTabCell.h"
@interface LastStageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *dataArray2;
@property (nonatomic,strong) UICollectionView *collec;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,strong) UITableView *tab;
@end

@implementation LastStageViewController

static NSString *const reuserID = @"LastStageCell";
static NSString *const reuserIDTab = @"LastStageTabCell";

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NSMutableArray *)dataArray2{
    if (_dataArray2 == nil) {
        
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    
    UIView *bgv = [[UIView alloc] initWithFrame:CGRectMake(10, 10, DEVICEWIDTH - 20, 30)];
    bgv.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgv];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0,DEVICEWIDTH - 20,30)];
    label.text = @"企业回访及成果追踪";
    
    label.textColor = [UIColor blueColor];
    
    label.font = [UIFont systemFontOfSize:15];
    
    [bgv addSubview:label];
    
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 4, 10)];
    vi.backgroundColor = [UIColor blueColor];
    
    [bgv addSubview:vi];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collec = [[UICollectionView alloc] initWithFrame:CGRectMake(10,40,DEVICEWIDTH - 20 ,250) collectionViewLayout:layout];
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake(DEVICEWIDTH-20,330);
    
    collec.dataSource = self;
    collec.delegate = self;
    self.collec = collec;
    collec.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    [self.view addSubview:collec];

    [collec registerNib:[UINib nibWithNibName:@"LastStageViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuserID];
    
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(10, 300, DEVICEWIDTH - 20, 30)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 0,DEVICEWIDTH - 20,30)];
    label2.text = @"座谈";
    
    label2.textColor = [UIColor blueColor];
    
    label2.font = [UIFont systemFontOfSize:15];
    
    [view2 addSubview:label2];
    
    UIView *vi2 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 4, 10)];
    vi2.backgroundColor = [UIColor blueColor];
    
    [view2 addSubview:vi2];

    UITableView *web = [[UITableView alloc] init];
    self.tab = web;
    web.delegate = self;
    web.dataSource = self;
    web.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:web];
    [web mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view2.mas_bottom);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-20);
    }];

    [self loadData];
    [self loadData2];
    
}

//企业回访接口
- (void)loadData{
    
    self.currentPage = 1;
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyhfMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"reason"];
        
        if (arr.count) {
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                
                LastStageModel *model = [LastStageModel mj_objectWithKeyValues:dict2];
                
                [self.dataArray addObject:model];
            }

        }
    
        [self.collec reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

//座谈接口

- (void)loadData2{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': ztMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        NSDictionary *dict = JSON[@"reason"];

        NSDictionary *dict2 = dict[@"attrs"];
        
        if (dict2) {
            LastStageTabModel *model = [LastStageTabModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray2 addObject:model];

        }
        [self.tab reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}


#pragma mark -----collectionView 数据源

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LastStageModel *model = self.dataArray[indexPath.row];
    
    LastStageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuserID forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];
    if (cell == nil) {
        
        cell = [LastStageViewCell WithLastStageViewCell];
    }

    cell.model = model;
    
    return cell;
}

#pragma mark------tableview 数据源

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    LastStageTabModel *model = self.dataArray2.firstObject;
    
    LastStageTabCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserIDTab];

    if (cell == nil) {
        
        cell = [LastStageTabCell WithLastStageTabCell];
    }
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 163;
}

@end














